package com.ruoyi.device.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.device.domain.TmdDeviceInformation;
import com.ruoyi.device.service.ITmdDeviceInformationService;
import com.ruoyi.device.vo.AllDeviceInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.device.domain.TmdDeviceRecord;
import com.ruoyi.device.service.ITmdDeviceRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 使用记录Controller
 * 
 * @author ruoyi
 * @date 2024-05-02
 */
@Controller
@RequestMapping("/Use/record")
public class TmdDeviceRecordController extends BaseController
{
    private String prefix = "Use/record";

    @Autowired
    private ITmdDeviceRecordService tmdDeviceRecordService;
    @Autowired
    private ITmdDeviceInformationService tmdDeviceInformationService;

    @RequiresPermissions("Use:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询使用记录列表
     */
//    @RequiresPermissions("Use:record:list")
//    @PostMapping("/list")
//    @ResponseBody
//    public TableDataInfo list(AllDeviceInfoVo allDeviceInfoVo)
//    {
//        startPage();
//        List<AllDeviceInfoVo> list = tmdDeviceRecordService.selectTmdDeviceRecordList(allDeviceInfoVo);
//        return getDataTable(list);
//    }
    /**
     * 查询使用记录列表
     */
    @RequiresPermissions("Use:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AllDeviceInfoVo allDeviceInfoVo)
    {
        startPage();
        SysUser currentUser = ShiroUtils.getSysUser();
        Long userId = currentUser.getUserId();
        if (currentUser.isAdmin()){
            List<AllDeviceInfoVo> list = tmdDeviceRecordService.selectTmdDeviceRecordList(allDeviceInfoVo);
            return getDataTable(list);
        }else{
            List<AllDeviceInfoVo> list = tmdDeviceRecordService.selectTmdDeviceRecordListByUserId(userId);
            return getDataTable(list);
        }
    }

    /**
     * 新增使用记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存使用记录
     */
    @RequiresPermissions("Use:record:add")
    @Log(title = "使用记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdDeviceRecord tmdDeviceRecord)
    {
        return toAjax(tmdDeviceRecordService.insertTmdDeviceRecord(tmdDeviceRecord));
    }

    /**
     * 修改使用记录
     */
    @RequiresPermissions("Use:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TmdDeviceRecord tmdDeviceRecord = tmdDeviceRecordService.selectTmdDeviceRecordById(id);
        mmap.put("tmdDeviceRecord", tmdDeviceRecord);
        return prefix + "/edit";
    }

    @RequiresPermissions("Use:record:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        AllDeviceInfoVo allDeviceInfoVo = tmdDeviceRecordService.selectAllDeviceInfoVoById(id);
        mmap.put("allDeviceInfoVo", allDeviceInfoVo);
        return prefix + "/detail";
    }

    /**
     * 修改保存使用记录
     */
    @RequiresPermissions("Use:record:edit")
    @Log(title = "使用记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdDeviceRecord tmdDeviceRecord)
    {
        return toAjax(tmdDeviceRecordService.updateTmdDeviceRecord(tmdDeviceRecord));
    }

    /**
     * 删除使用记录
     */
    @RequiresPermissions("Use:record:remove")
    @Log(title = "使用记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tmdDeviceRecordService.deleteTmdDeviceRecordByIds(ids));
    }
}
